package com.ruoyi.act.module.controller.act;

import com.github.pagehelper.PageInfo;
import com.ruoyi.act.module.domain.ActDeploy;
import com.ruoyi.act.module.service.IDeployService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 流程管理
 *
 * @author hcj
 */
@RestController
@RequestMapping("/act/deploy")
public class DeploymentController extends BaseController {

    @Autowired
    private IDeployService iDeployService;

    /**
     * 分页查询部署的流程
     *
     * @param deploy
     * @return
     */
    @PostMapping("/list")
    public PageInfo findPage(@RequestBody ActDeploy deploy) {
        PageInfo<Deployment> pageInfo = iDeployService.findPage(deploy);
        return pageInfo;
    }

    /**
     * 根据模型id发布流程
     *
     * @param modelId
     * @return
     * @throws IOException
     */
    @GetMapping("/{modelId}")
    public AjaxResult deploy(@PathVariable String modelId) throws IOException {
        Deployment deployment = iDeployService.createDeploymentByModel(modelId);
        if (deployment != null) {
            return AjaxResult.success(deployment);
        }
        return AjaxResult.error();
    }

    /**
     * 删除部署的流程
     *
     * @param deploy
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ActDeploy deploy) {

        String s = deploy.getDeploymentId();
        String[] s1 = s.split(",");

        if (s1.length > 1) {
            for (String s2 : s1) {
                deploy.setDeploymentId(s2);
                iDeployService.deleteDeployment(deploy);
            }
        } else {
            iDeployService.deleteDeployment(deploy);
        }


        return AjaxResult.success();
    }

}
